package com.cyh.testcustomui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class MyRatingBar extends View {
    private Bitmap mNormalBitmap;
    private Bitmap mFocusBitmap;
    private int mGradeNumber;
    private int mStarPadding;
    private int mCurrentGrade = 0;

    public MyRatingBar(Context context) {
        this(context, null);
    }

    public MyRatingBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyRatingBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyRatingBar);
        int normalId = array.getResourceId(R.styleable.MyRatingBar_starNormal, R.drawable.star_normal);
        int focusId = array.getResourceId(R.styleable.MyRatingBar_starFocus, R.drawable.star_focus);
        mGradeNumber = array.getInteger(R.styleable.MyRatingBar_gradeNumber, 5);
        mStarPadding = array.getDimensionPixelOffset(R.styleable.MyRatingBar_starPadding, 5);
        mNormalBitmap = BitmapFactory.decodeResource(getResources(), normalId);
        mFocusBitmap = BitmapFactory.decodeResource(getResources(), focusId);
        array.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int height = mFocusBitmap.getHeight();
        int width = mFocusBitmap.getWidth() * mGradeNumber + (mGradeNumber - 1) * mStarPadding; //+ 间隔
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for(int i = 0; i < mGradeNumber; i++) {
            int x = i * mFocusBitmap.getWidth() + i * mStarPadding;
            if(i < mCurrentGrade) {
                canvas.drawBitmap(mFocusBitmap, x, 0, null);
            } else {
                canvas.drawBitmap(mNormalBitmap, x, 0, null);
            }

        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            //case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
            //case MotionEvent.ACTION_UP:
                float moveX = event.getX(); //event.getX()相对于当前控件的位置，event.getRawX()相对于屏幕的位置
                int currentGrade = (int)(moveX / (mFocusBitmap.getWidth() + mStarPadding) + 1);
                if(currentGrade < 0) {
                    currentGrade = 0;
                }
                if(currentGrade > mGradeNumber) {
                    currentGrade = mGradeNumber;
                }
                if(mCurrentGrade != currentGrade) {
                    mCurrentGrade = currentGrade;
                    invalidate();
                }
        }
        return true; //false 不消费，第一次down 返回false, 后面的事件就进不来。
    }
}
